﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Skylabs.Net;

namespace Skylabs.Breeze
{
    public static class Server
    {
        public static TcpListener Listener { get; set; }
        public static List<Client> Clients { get; set; }
        public static List<FileTransfer> FileTransfers { get; set; }
        private static int curIndex = 0;
        public static void Start()
        {
            Program.ServerTrace.TraceInformation("Server Starting");
            Clients = new List<Client>();
            FileTransfers = new List<FileTransfer>();
            if(Listener == null)
                Listener = new TcpListener(IPAddress.Any, 7678);
            try
            {
                Listener.Start(1);
                AcceptClients();
            }
            catch (Exception e)
            {
                Program.ServerTrace.TraceEvent(TraceEventType.Error,0,e.Message);
            }
            
        }
        public static void ClientDone(Client c,string filename)
        {
            //Program.ServerTrace.TraceInformation("Client disconnected");
            c.Close(DisconnectReason.CleanDisconnect);
            Clients.Remove(c);
            if (Clients.Count == 0)
            {
                FileTransfers.Remove(FileTransfers.Where(t => t.FileName.Equals(filename)).First());
                Program.ServerTrace.TraceInformation("File '{0}' Transfered.",filename);
            }
        }
        public static void Stop()
        {
            Program.ServerTrace.TraceInformation("Server Stopping");
            try
            {
                Listener.Stop();
            }
            catch (Exception e)
            {
                Program.ServerTrace.TraceEvent(TraceEventType.Error, 0, e.Message);
            }
        }
        private static void AcceptClients()
        {
            Listener.BeginAcceptTcpClient(OnClientConnect, null);
        }
        private static void OnClientConnect(IAsyncResult o)
        {
            TcpClient c = Listener.EndAcceptTcpClient(o);
            Clients.Add(new Client(c,curIndex));
            curIndex++;
            if (curIndex == int.MaxValue)
                curIndex = 0;
            //Program.ServerTrace.TraceInformation("Client connected.");
            AcceptClients();
        }
    }
}
